home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / TEST_REG.C < prev    next >
C/C++ Source or Header  |  1992-08-11  |  9KB  |  267 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12.  
  13. #include <cool/Regexp.h>
  14. #include <test.h>
  15.  
  16. #if defined(DOS)
  17. extern "C" {
  18. #include <string.h>
  19. }
  20. #else
  21. #include <string.h>
  22. #endif
  23.  
  24. void test_regexp () {
  25.   // GENERAL TEST OF MEMBER FUNCTIONS FOR REGEXP CLASS.
  26.  
  27.   // Test the CoolRegexp(char*) constructor [and thus also the compile(char*)
  28.   // member function], and do a very basic test on the find(char*) function.
  29.  
  30.   CoolRegexp rd1("Hi There");
  31.   TEST ("rd1.is_valid()",rd1.is_valid(),TRUE);
  32.   rd1.set_invalid();
  33.   TEST ("rd1.is_valid()",rd1.is_valid(),FALSE);
  34.   CoolRegexp rrr;
  35.   TEST ("rrr.is_valid()",rrr.is_valid(),FALSE);
  36.   rrr.compile("hello");
  37.   TEST ("rrr.is_valid()",rrr.is_valid(),TRUE);
  38.  
  39.   CoolRegexp r1("Hi There");
  40.   char* strng = "garbage49r*%Hi Therean d more Gsarbage";
  41.   TEST ("CoolRegexp r1(Hi There)", 0, 0);
  42.   TEST ("r1.find(It is not here)", r1.find("It is not here"), 0);
  43.   TEST ("r1.find(strng)", r1.find(strng), 1);
  44.   TEST ("r1.start()",r1.start(), 12);
  45.   TEST ("r1.end",r1.end(), 20);
  46.  
  47.   // Test the CoolRegexp(&CoolRegexp) constructor, and the == and deep_equal
  48.   // functions.
  49.  
  50.   CoolRegexp r2(r1);
  51.   TEST ("CoolRegexp r2(r1)", 0, 0);
  52.   TEST ("r1==r2", r1==r2, 1);
  53.   TEST ("r2==r1", r2==r1, 1);
  54.   TEST ("r2.deep_equal(r1)", r2.deep_equal(r1), 1);
  55.   TEST ("r1.deep_equal(r2)", r1.deep_equal(r2), 1);
  56.   TEST ("r2.find(strng)", r2.find(strng), 1);
  57.   TEST ("r1==r2", r1==r2, 1);
  58.   TEST ("r2==r1", r2==r1, 1);
  59.   TEST ("r2.deep_equal(r1)", r2.deep_equal(r1), 1);
  60.   TEST ("r1.deep_equal(r2)", r1.deep_equal(r2), 1);
  61.   TEST ("r2.find(Its not here)", r2.find("Its not here"), 0); 
  62.   TEST ("r1==r2", r1==r2, 1);
  63.   TEST ("r2==r1", r2==r1, 1);
  64.   TEST ("r2.deep_equal(r1)", r2.deep_equal(r1), 1);
  65.   TEST ("r1.deep_equal(r2)", r1.deep_equal(r2), 1);
  66.   TEST ("r2.find(Another Hi Therestring.)", 
  67.     r2.find("Another Hi Therestring."), 1);
  68.   TEST ("r1==r2", r1==r2, 1);
  69.   TEST ("r2==r1", r2==r1, 1);
  70.   TEST ("r1!=r2", r1!=r2, 0);
  71.   TEST ("r2!=r1", r2!=r1, 0);
  72.   TEST ("r2.deep_equal(r1)", r2.deep_equal(r1), 0);
  73.   TEST ("r1.deep_equal(r2)", r1.deep_equal(r2), 0);
  74.  
  75.   // Tests the CoolRegexp() constructor and finishes up the testing on
  76.   // the == operator.
  77.  
  78.   CoolRegexp r3;
  79.   r3.compile("hELl");
  80.   char* strng2 = "o, hELlo";
  81.   char* strng3 = "I don't think its here";
  82.   TEST ("CoolRegexp r3()", 0, 0);
  83.   TEST ("r3.compile(hELl)", 0, 0);
  84.   TEST ("r3.find(strng2)", r3.find(strng2), 1);
  85.   TEST ("r1==r3", r1==r3, 0);
  86.   TEST ("r1.deep_equal(r3)", r1.deep_equal(r3), 0);
  87.   TEST ("r3.start()",r3.start(), 3);
  88.   TEST ("r3.end()",r3.end(), 7);
  89.   CoolRegexp r4(r3);
  90.   TEST ("r3.find(strng3)", r3.find(strng3), 0);
  91.   TEST ("r4==r3", r4==r3, 1);
  92.   TEST ("r4.deep_equal(r3)", r4.deep_equal(r3), 0);
  93.   
  94.  
  95.  
  96.  
  97.  
  98.   ////
  99.   //
  100.   // THIS COMPLETES THE GENERAL TESTS OF THE MEMBER FUNCTIONS.
  101.   // 
  102.   // THE FOLLOWING TESTS ARE FOR COMPILE AND FIND, AND TEST
  103.   // VARIOUS POSSIBILITIES OF REGULAR EXPRESSIONS.
  104.   //
  105.   //// 
  106.  
  107.  
  108. // A regular expression allows a programmer to specify complex patterns that
  109. // can be searched for and matched against the character string of a String
  110. // object.  In its simplest case, a regular expression is a sequence of
  111. // characters with which you can search for exact character matches.  However,
  112. // many times you may not know the exact sequence you want to find, or you may
  113. // only want to find a match at the beginning or end of a String.  The CoolRegexp
  114. // object allows specification of such patterns by utilizing the following
  115. // regular expression
  116.  
  117. //meta-characters:
  118. //
  119. //         ^    Match at beginning of line
  120. //         $    Match at end of line
  121. //         .    Match any single character
  122. //         [ ]  Match any one character inside the brackets
  123. //         -    Match any character in range on either side of dash
  124. //         *    Match preceding pattern zero or more times
  125. //         +    Match preceding pattern one or more times
  126. //         ?    Match preceding pattern zero or once only
  127. //         ()   Save a matched expression and use it in a further match.
  128.  
  129. // BELOW ARE TESTS FOR THE REGULAR EXPRESSIONS USING THE SYMBOLS LISTED
  130. // ABOVE.  THEY PROVIDE GOOD EXAMPLES OF REGULAR EXPRESSION USE.  
  131.  
  132. // OTHER GOOD EXAMPLES OF HOW REGULAR EXPRESSIONS ARE USED, CAN BE
  133. // FOUND IN DOCUMENTATION FOR ED, GREP, AND AWK.
  134.  
  135. // THE ABOVE SYMBOLS CAN BE USED TOGETHER TO FORM COMPLEX REGULAR EXPRESSIONS
  136. // FOR VERY UNUSUAL MATHING.  THERE ARE A FEW EXAMPLES OF THIS TYPE OF
  137. // USE IN THE FOLLOWING TESTS.  --MNF
  138.  
  139.  
  140. // TESTS FOR REGULAR EXPRESSIONS WITH ^
  141.  
  142.   char* s = "str at front";
  143.   CoolRegexp rxp("^str");
  144.   TEST ("CoolRegexp rxp(^str)", 0, 0);
  145.   TEST ("rxp.find(str at front)", rxp.find(s), 1);
  146.   TEST ("rxp.start()", rxp.start(), 0);
  147.   TEST ("rxp.end()", rxp.end(), 3);
  148.   TEST ("rxp.find(Str not present)", rxp.find("Str not present"), 0);
  149.   TEST ("rxp.find(not at str front)", rxp.find("not at str front"), 0);
  150.   TEST ("rxp.find(at end str)", rxp.find("at end str"), 0);
  151.  
  152.  
  153. // TESTS FOR REGULAR EXPRESSIONS WITH $
  154.   
  155.   rxp.compile("str$");
  156.   TEST ("rxp.compile(str$)", 0, 0);
  157.   TEST ("rxp.find(str at front)", rxp.find("str at front"), 0);
  158.   TEST ("rxp.find(Str not present)", rxp.find("Str not present"), 0);
  159.   TEST ("rxp.find(not at str end)", rxp.find("not at str end"), 0);
  160.   TEST ("rxp.find(at end str\0)", rxp.find("at end str\0"), 1);
  161.  
  162.   
  163. // TESTS FOR REGULAR EXPRESSIONS WITH .
  164.  
  165.   rxp.compile("s..t.r");
  166.   TEST ("rxp.compile(s..t.r)", 0, 0);
  167.   TEST ("rxp.find(so three)", rxp.find("so three"), 1);
  168.   TEST ("rxp.find(str too close)", rxp.find("str too close"), 0);
  169.   TEST ("rxp.find(dl 32 s*4t0r ugh)", rxp.find("dl 32 s*4t0r ugh"), 1);
  170.   TEST ("rxp.find(too far s  t  r)", rxp.find("too far s  t  r"), 0);
  171.   
  172.  
  173. // TESTS FOR REGULAR EXPRESSIONS WITH [] [^  ] and -
  174.  
  175.   s = "ugh b is not 1";
  176.   rxp.compile("[ab1-9]");
  177.   TEST ("rxp.compile([ab1-9])", 0, 0);
  178.   TEST ("rxp.find(6 is the num)", rxp.find("6 is the num"), 1);
  179.   TEST ("rxp.find(ugh b is not 1)", rxp.find(s), 1);
  180.   TEST ("rxp.start()",rxp.start(),4);
  181.   TEST ("rxp.end()", rxp.end(),5);
  182.   TEST ("rxp.find(this will not m'tch)", rxp.find("this will not m'tch"), 0);
  183.   
  184.   s = "ab123QQ59ba";
  185.   rxp.compile("[^ab1-9]");
  186.   TEST ("rxp.compile([^ab1-9])", 0, 0);
  187.   TEST ("rxp.find(ab123QQ59ba)", rxp.find(s), 1);
  188.   TEST ("rxp.start()",rxp.start(),5);
  189.   TEST ("rxp.end()", rxp.end(),6);
  190.   TEST ("rxp.find(ab123456789ba)", rxp.find("ab123456789ba"), 0);
  191.  
  192.  
  193. // TESTS FOR REGULAR EXPRESSIONS WITH *, +, and ?
  194.  
  195.   s = "rrra   lddd";
  196.   rxp.compile("a *l");
  197.   TEST ("rxp.compile(a *l)",0,0);
  198.   TEST ("rxp.find(a *l)", rxp.find(s), 1);
  199.   TEST ("rxp.start()",rxp.start(),3);
  200.   TEST ("rxp.end()", rxp.end(),8);
  201.   TEST ("rxp.find(hello there)", rxp.find("hello there"), 0);
  202.   TEST ("rxp.find(<<<al>>>)", rxp.find("<<<al>>>"), 1);
  203.  
  204.   rxp.compile("a +l");
  205.   TEST ("rxp.compile(a +l)",0,0);
  206.   TEST ("rxp.find(a *l)", rxp.find(s), 1);
  207.   TEST ("rxp.start()",rxp.start(),3);
  208.   TEST ("rxp.end()", rxp.end(),8);
  209.   TEST ("rxp.find(hello there)", rxp.find("hello there"), 0);
  210.   TEST ("rxp.find(<<<al>>>)", rxp.find("<<<al>>>"), 0);
  211.  
  212.   rxp.compile("a ? ? ?l");
  213.   TEST ("rxp.compile(a ?l)",0,0);
  214.   TEST ("rxp.find(a *l)", rxp.find(s), 1);
  215.   TEST ("rxp.start()",rxp.start(),3);
  216.   TEST ("rxp.end()", rxp.end(),8);
  217.   TEST ("rxp.find(hello there)", rxp.find("hello there"), 0);
  218.   TEST ("rxp.find(<<<al>>>)", rxp.find("<<<al>>>"), 1);
  219.  
  220. // TESTS FOR REGULAR EXPRESSIONS WITH TEMPORARY STORAGE i.e. ()
  221.  
  222.   // finds an expression ending with pb and beginning with whatever
  223.   // the two characters before the first p encounterd in the line were.
  224.  
  225.   s = "rep drepa qrepb";
  226.   rxp.compile("(..p)b");
  227.   TEST ("rxp.compile((..p)b)", 0, 0);
  228.   TEST ("rxp.find(rep drepa qrepb)", rxp.find(s), 1);
  229.   TEST ("rxp.start()", rxp.start(), 11);
  230.   TEST ("rxp.end()", rxp.end(), 15);
  231.   TEST ("rxp.find(repreprep)", rxp.find("repreprep"), 0);
  232.  
  233.  
  234.  
  235.   // finds an expression ending with p, beginning with b, and having 
  236.   // two characters in between that are the same as the two characters
  237.   // before the first p encounterd in the line.
  238.  
  239.   rxp.compile("d(..p)");
  240.   TEST ("rxp.compile(d(..p))", 0, 0);
  241.   TEST ("rxp.find(rep drepa qrepb)", rxp.find(s), 1);
  242.   TEST ("rxp.start()",rxp.start(), 4);
  243.   TEST ("rxp.end()",rxp.end(),8);
  244.   TEST ("rxp.find(repreprep)", rxp.find("repreprep"), 0);
  245.  
  246.   s = "Hi there \n this is another \n line";
  247.   rxp.compile("Hi.*line");
  248.   TEST ("find",rxp.find(s),1);
  249. }
  250.  
  251. void test_leak() {
  252.   for (;;) {
  253.     test_regexp();
  254.   }
  255. }
  256.  
  257. int main (void) {
  258.   START("CoolRegexp");
  259.   test_regexp();
  260. #if LEAK
  261.   test_leak();
  262. #endif
  263.   SUMMARY();
  264.   return 0;
  265. }
  266.  
  267.